<?php

require_once dirname(__FILE__).'/../lib/participationGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/participationGeneratorHelper.class.php';

/**
 * participation actions.
 *
 * @package    sf_sandbox
 * @subpackage participation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class participationActions extends autoParticipationActions
{
  public function executeNextstatus(sfWebRequest $request)
  {
    $this->forward404Unless($this->participation = Doctrine::getTable('Participation')->find(array($request->getParameter('id'))), sprintf('Object participation does not exist (%s).', array($request->getParameter('id'))));
    $this->participation->incrementStatus();    
    $this->getUser()->setFlash('notice', 'Participation of '.$this->participation->getRecepient().' was set to "'.$this->participation->getStatus().'".');
    $this->redirect($request->getReferer());
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->forward404Unless($this->participation = Doctrine::getTable('Participation')->find(array($request->getParameter('id'))), sprintf('Object participation does not exist (%s).', array($request->getParameter('id'))));
    
    //$this->form = new JournalEntryForm($this->journal_entry);

    $this->item = new ParticipationDetail();
    $this->item['participation_id']=$this->participation["id"];
    $this->itemform = new ParticipationDetailForm($this->item);
    
    $this->items=$this->participation->getParticipationDetails();
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $this->getUser()->setFlash('notice', $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.');

      $participation = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $participation)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $this->getUser()->getFlash('notice').' You can add another one below.');

        $this->redirect('@participation_new');
      }
      else
      {
        $this->redirect('@participation_show?id='.$participation['id']);
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.');
    }
  }
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

    $item=$this->getRoute()->getObject();
    $item->cascadeDelete();

    $this->getUser()->setFlash('notice', 'The item was deleted successfully.');

    $this->redirect($request->getReferer());
  }
  public function executeAdd(sfWebRequest $request)
  {
    $part=new Participation();
    $part["batch_id"]=$request->getParameter("batch_id");;
    $part["recepient_id"]=$request->getParameter("recepient_id");
    $part["status"]="Pending";
    $part->save();
    foreach($part->getBatch()->getProgram()->getInputs() as $input)
    {
      $partdetail=new ParticipationDetail();
      $partdetail->setParticipationId($part["id"]);
      $partdetail->setInputId($input["id"]);
      $partdetail->setQty($input["qty"]);
      $partdetail->setPrice($input["price"]);
      $partdetail->save();
    }

    $this->getUser()->setFlash('notice', 'The item was created successfully.');

    $this->redirect($request->getReferer());
  }
  public function executeSetdeliverydate(sfWebRequest $request)
  {
    $this->forward404Unless($part = Doctrine::getTable('Participation')->find(array($request->getParameter('id'))), sprintf('Object participation does not exist (%s).', array($request->getParameter('id'))));
    $part["inputdeliverydate"]=
      $request->getParameter("participation[inputdeliverydate][year]").
      "-".
      $request->getParameter("participation[inputdeliverydate][month]").
      "-".
      $request->getParameter("participation[inputdeliverydate][day]");
    $part->save();

    $this->getUser()->setFlash('notice', 'The item delivery set to '.MyDateTime::frommysql($part["inputdeliverydate"])->toprettydate());

    $this->redirect($request->getReferer());
  }
}
